package com.ruoyi.docdraw.service.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.docdraw.domain.SysDocDraw;
import com.ruoyi.docdraw.mapper.SysDocMapper;
import com.ruoyi.docdraw.domain.SysDoc;
import com.ruoyi.docdraw.service.ISysDocService;

/**
 * 文档Service业务层处理
 * 
 * @author lvtian
 * @date 2025-02-26
 */
@Service

public class SysDocServiceImpl implements ISysDocService 
{
    private static final Logger logger = LoggerFactory.getLogger(SysDocServiceImpl.class);
    @Autowired
    private SysDocMapper sysDocMapper;

    /**
     * 查询文档
     * 
     * @param docId 文档主键
     * @return 文档
     */
    @Override
    public SysDoc selectSysDocByDocId(Long docId)
    {
        return sysDocMapper.selectSysDocByDocId(docId);
    }
    /**
     * 文档按版本排序方法
     * @param docList
     * @return
     */
    @Override
    public List<SysDoc> sortByVersion(List<SysDoc> docList) {
        docList.sort((doc1, doc2) -> {
            // 比较 versionIdentifier
            int versionCompare = doc2.getVersionIdentifier().compareTo(doc1.getVersionIdentifier());
            if (versionCompare != 0) {
                return versionCompare;
            }
            // 如果 versionIdentifier 相同，比较 iterationIdentifier
            int iteration1 = Integer.parseInt(doc1.getIterationIdentifier());
            int iteration2 = Integer.parseInt(doc2.getIterationIdentifier());
            return Integer.compare(iteration2, iteration1);
        });
        return docList;
    }

    /**
     * 查询文档列表
     * 
     * @param sysDoc 文档
     * @return 文档
     */
    @Override
    public List<SysDoc> selectSysDocList(SysDoc sysDoc)
    {
        return sysDocMapper.selectSysDocList(sysDoc);
    }

    /**
     * 新增文档
     * 
     * @param sysDoc 文档
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysDoc(SysDoc sysDoc)
    {
//        sysDoc.setCreateTime(DateUtils.getNowDate());
        int rows = sysDocMapper.insertSysDoc(sysDoc);
        insertSysDocDraw(sysDoc);
        return rows;
    }

    /**
     * 修改文档
     * 
     * @param sysDoc 文档
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysDoc(SysDoc sysDoc)
    {
//        sysDoc.setUpdateTime(DateUtils.getNowDate());
        sysDocMapper.deleteSysDocDrawByDocId(sysDoc.getDocId());
        insertSysDocDraw(sysDoc);
        return sysDocMapper.updateSysDoc(sysDoc);
    }

    /**
     * 批量删除文档
     * 
     * @param docIds 需要删除的文档主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysDocByDocIds(Long[] docIds)
    {
        sysDocMapper.deleteSysDocDrawByDocIds(docIds);
        return sysDocMapper.deleteSysDocByDocIds(docIds);
    }

    /**
     * 删除文档信息
     * 
     * @param docId 文档主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysDocByDocId(Long docId)
    {
        sysDocMapper.deleteSysDocDrawByDocId(docId);
        return sysDocMapper.deleteSysDocByDocId(docId);
    }

    /**
     * 新增文档图纸信息
     * 
     * @param sysDoc 文档对象
     */
    public void insertSysDocDraw(SysDoc sysDoc)
    {
        List<SysDocDraw> sysDocDrawList = sysDoc.getSysDocDrawList();
        logger.info("sysDocDrawList: {}", sysDocDrawList); // 打印 sysDocDrawList
        Long docId = sysDoc.getDocId();
        if (StringUtils.isNotNull(sysDocDrawList)) {
            List<SysDocDraw> list = new ArrayList<>();
            for (SysDocDraw sysDocDraw : sysDocDrawList) {
                sysDocDraw.setDocId(docId);
                list.add(sysDocDraw);
            }
            if (list.size() > 0) {
                int result = sysDocMapper.batchSysDocDraw(list);
                logger.info("batchSysDocDraw 执行结果: {}", result); // 打印执行结果
            }
        }
    }

    @Override
    public SysDoc selectSysDocByDocOr(String docOr) {
        return sysDocMapper.selectSysDocByDocOr(docOr);
    }


    @Override
    public int deleteSysDocByDocMasterId(Long docMasterId) {
        return sysDocMapper.deleteSysDocByDocMasterId(docMasterId);
    }
}
